<template>
  <el-dialog
    :title="!dataForm.appointmentId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="房源id" prop="houseId">
      <el-input v-model="dataForm.houseId" placeholder="房源id"></el-input>
    </el-form-item>
    <el-form-item label="类型 1-新楼盘  2-二手房  3-出租房" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型 1-新楼盘  2-二手房  3-出租房"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="uid">
      <el-input v-model="dataForm.uid" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="经纪人id" prop="brokerId">
      <el-input v-model="dataForm.brokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    <el-form-item label="状态 0-未受理 1-已受理" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态 0-未受理 1-已受理"></el-input>
    </el-form-item>
    <el-form-item label="经纪人联系方式" prop="brokerPhone">
      <el-input v-model="dataForm.brokerPhone" placeholder="经纪人联系方式"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          appointmentId: 0,
          houseId: '',
          type: '',
          uid: '',
          brokerId: '',
          status: '',
          brokerPhone: ''
        },
        dataRule: {
          houseId: [
            { required: true, message: '房源id不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型 1-新楼盘  2-二手房  3-出租房不能为空', trigger: 'blur' }
          ],
          uid: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          brokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 0-未受理 1-已受理不能为空', trigger: 'blur' }
          ],
          brokerPhone: [
            { required: true, message: '经纪人联系方式不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.appointmentId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.appointmentId) {
            this.$http({
              url: this.$http.adornUrl(`/behavior/appointment/info/${this.dataForm.appointmentId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.houseId = data.appointment.houseId
                this.dataForm.type = data.appointment.type
                this.dataForm.uid = data.appointment.uid
                this.dataForm.brokerId = data.appointment.brokerId
                this.dataForm.status = data.appointment.status
                this.dataForm.brokerPhone = data.appointment.brokerPhone
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/behavior/appointment/${!this.dataForm.appointmentId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'appointmentId': this.dataForm.appointmentId || undefined,
                'houseId': this.dataForm.houseId,
                'type': this.dataForm.type,
                'uid': this.dataForm.uid,
                'brokerId': this.dataForm.brokerId,
                'status': this.dataForm.status,
                'brokerPhone': this.dataForm.brokerPhone
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
